﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
 
using System.Xml;
using System.Diagnostics;


namespace FTchina.SiteConfig
{
    class XmlConfig
    {
        private string xmlPath;
        private bool isConfig;
        private XmlDocument xmlDocument;


        /// <summary>
        /// 构造函数：装载配置文件
        /// </summary>
        /// <param name="xmlPath">配置文件的路径</param>
        public XmlConfig(string xmlPath)
        {
             

            this.xmlDocument = new XmlDocument();
            this.XmlPath = xmlPath;
        }

        /// <summary>
        /// Xml配置文件的路径
        /// </summary>
        public string XmlPath
        {
            set
            {
                xmlPath = value;
                isConfig = OnXmlPathChange();
            }
        }

        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="value">返回的键值</param>
        /// <returns>键值</returns>
        public string ReadConfig(string section, string key)
        {
            string tempwords = string.Empty;

            try
            {
                if (isConfig)
                {
                    if (FTchina.Caching.FTchinaCache.IsExits(section + key))
                    {
                        tempwords = HttpRuntime.Cache.Get(section + key).ToString();
                    }
                    else
                    {
                        tempwords = xmlDocument.SelectSingleNode("Configuration").SelectSingleNode(section).SelectSingleNode(key).InnerText;
                        HttpRuntime.Cache.Insert(section + key, tempwords);
                    }
                }
            }
            catch
            {

            }
           
                return tempwords;
           
        }

        /// <summary>
        /// 写入配置
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="value">要写入的新键值</param>
        /// <returns>是否写入成功</returns>
        public bool WriteConfig(string section, string key, string value)
        {
            bool isWrited = false;
            try
            {
                if (isConfig)
                {
                    xmlDocument.SelectSingleNode("Configuration").SelectSingleNode(section).SelectSingleNode(key).InnerText = value;
                    xmlDocument.Save(xmlPath);
                    isWrited = true;
                }
            }
            catch
            {
               
            }
            return isWrited;
        }

        #region 私有方法
        private bool OnXmlPathChange()
        {
            bool isLoaded = false;
            try
            {
                xmlDocument.Load(xmlPath);
                isLoaded = true;
            }
            catch { }
            return isLoaded;
        }
        #endregion
    }

}